/**
    登录页面
*/

<template>
    <div>
        <div>用户名：<el-input v-model="form.username" style="width:300px"></el-input></div>
        <div>密码：<el-input type="password" v-model="form.password" style="width:300px"></el-input></div>
        <div> <el-checkbox v-model="checked">记住密码</el-checkbox></div>
        <div><el-button type="danger" @click="eventLogin">立即登录</el-button></div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            checked: true,
            form:{
                username:'',
                password:''
            }
        };
    },
    mounted(){

        //是否记住密码，就是看缓存中是否有数据，所以先把缓存的数据拿出来
        let u = this.$cookies.get('2104a_three_login_name')
        let p = this.$cookies.get('2104a_three_login_password')

        if(u!=null && p != null){
            this.form.username = u
            this.form.password = p
        }

    },
    methods: {
        //登录按钮事件
        eventLogin() {
            let url = this.$baseUrl + '/system/user/login'
            let data = {
                "id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
                "name": "string",
                "loginName": this.form.username,
                "password": this.form.password,
                "state": "string",
                "isDeleted": true,
                "createdDate": "2023-09-14T08:35:29.325Z"
            }
            this.$http.post(url, data).then(res => {
                console.log(res);
                this.$message.info(res.data.message)
                if(res.data.state){
                    //登录成功
                    if(this.checked){ //表示勾先了记住密码。
                        //记住密码了应该做什么？
                        //把用户名和密码保存下，我们保存在cookie中
                        /***
                         * 1、安装cookie：npm install vue-cookies -S
                         * 2、全局配置：在main.js中
                         *     import cookie from 'vue-cookies' //引入cookie
                         *     Vue.prototype.$cookies = cookie //取别名，方便在页面中操作
                         * 3、直接在页面中使用
                         *   1）把数据放到cookie中，用set
                         *   2）把数据从cookie中拿出来，用get
                         */
                        this.$cookies.set('2104a_three_login_name',this.form.username,12*60*60)
                        this.$cookies.set('2104a_three_login_password',this.form.password,12*60*60)

                        //跳转到列表
                        this.$router.push('/three/system/list')
                    }
                }
            })
            .catch(err=>{
                if(err.code == 'ERR_NETWORK'){
                    this.$message.error('网络连接失败，请检查后重试！')
                }
            })
        }
    }

};
</script>
